var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    show: false,
    count: 1,
    color: ['黑色', '白色'],
    price_list: [20, 20, 45],
    list: [],

    current_options:{},
    total: 0,
    title: "",
    has_many_specs: [],
    has_many_options:[],
    thumb: "",
    id: 0,
    goods: [],
    give_active_value: '',
    shop_package_id: "",

    specs: "" //多规格商品id
  },
  close() {
    this.setData({
      show: false
    })
  },
  choose(e) {
    console.log( e.currentTarget.dataset)
    let {
      i,
      item: specs_item,
      c_item
    } = e.currentTarget.dataset;
    let current_specs = ''; //多规格id

    this.data.list.forEach(item => {
      if (item.id == this.data.id) {
        item.has_many_specs.forEach((chi, chi_index) => {
          if (chi.id == specs_item.id) {
            chi.index = i;
            let _arr = item.current_options.specs.split("_");
            _arr[chi_index] = c_item.id + '';
            current_specs = _arr.join("_");
          }
        })
        item.has_many_options.forEach(options_item => {
          if (options_item.specs == current_specs) {

            item.current_options = options_item
            this.data.current_options = options_item
          }
        })
      }
    })
    this.data.has_many_specs.forEach(item => {
      if (item.id == specs_item.id) {
        item.index = i;
      }
    })
    this.setData({
      list: this.data.list,
      has_many_specs: this.data.has_many_specs
    })

    let options_shop = this.data.has_many_options.filter(item=>item.specs == current_specs);
    this.data.goods.forEach(item => {
      if(item.goods_id == this.data.id){
        item.option_id = options_shop[0].id
      }
    })

    this.setData({goods:this.data.goods,current_options:this.data.current_options})

    this.total_price()
  },
  minus() {
    if (this.data.count > 1) {
      this.data.count -= 1;
      this.data.goods.forEach(item => {
        item.total-=1;
      })
      this.setData({
        count: this.data.count,
        goods:this.data.goods
      })
      this.total_price()
    }
  },
  specification(e) {
    let data = e.currentTarget.dataset.item;
    this.setData({
      show: true,
      title: data.title,
      has_many_specs: data.has_many_specs,
      thumb: data.thumb,
      has_many_options:data.has_many_options,
      current_options:data.current_options,
      id: data.id
    })
  },
  pay() {
    let goodsId = []
    this.data.goods.forEach(item => {
      goodsId.push(item.goods_id)
    })
    goodsId =goodsId.join()
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?tag=sign_buy&package_id=${this.data.shop_package_id}&goodsId=${goodsId}&goods=${JSON.stringify(this.data.goods)}`
    });
  },
  plus() {
    this.data.count += 1;
    this.data.goods.forEach(item=>{
      item.total+=1;
    })
    this.setData({
      count: this.data.count,
      goods:this.data.goods
    });
    this.total_price()

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  total_price() {
    let no_options_goods_price = this.data.list.filter(item => item.has_option == 0).reduce((pre, cur) => pre + Number(cur.price), 0),
      options_goods_price = this.data.list.filter(item => item.has_option == 1).reduce((pre, cur) => pre + Number(cur.current_options.product_price), 0)

    let total = (no_options_goods_price + options_goods_price) * this.data.count;

    this.setData({
      total
    })
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.sign-buy.frontend.package.index.home"),
      success: (resdata) => {
        let {
          result,
          msg,
          data: {
            goods_list,
            give_active_value,
            shop_package_id
          }
        } = resdata.data;
        if (result == 0) return app.tips(msg);
        console.log(shop_package_id)
        this.setData({
          list: goods_list,
          give_active_value,
          shop_package_id
        })
        this.data.list.forEach(item => {
          item.has_many_specs.forEach(c_item => {
            c_item.index = 0;
          })
          item.current_options = item.has_many_options[0]
          if (item.has_option == 1) {
            this.data.goods.push({
              'goods_id': item.id,
              'total': 1,
              'option_id': item.has_many_specs[0].id
            })
          }
          if (item.has_option == 0) {
            this.data.goods.push({
              'goods_id': item.id,
              'total': 1,
              'option_id': 0
            })
          }
        })
        this.setData({
          list: this.data.list,
          goods: this.data.goods
        });
        this.total_price()
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})